package com.recruit.control;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.recruit.dao.gen.model.ResumeFile;
import com.recruit.dao.gen.model.User;
import com.recruit.service.ResumeService;

@Controller
public class PdfController {

	@Autowired
	ResumeService resumeService;
	
	@RequestMapping("/pdfView/{fileId}/{fileName}")
	public void pdfView(@PathVariable("fileId")String fileId, HttpServletRequest request, HttpServletResponse response) {
//		String root = request.getSession().getServletContext().getRealPath("/");
		String root = "/tempfile/";
		ResumeFile resumeFile = resumeService.getResumeInfoByFileId(fileId);
		String fileName = resumeFile.getFileName();
		
		String filePath = root + "pdf" + File.separator + fileName; 
//		String filePath = resumeFile.getFilePath();
		
		try {
			IOUtils.copy(new FileInputStream(filePath), response.getOutputStream());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/pdf/upload")
	@ResponseBody
	public String pdfUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request,@RequestParam("resumeName")String resumeName) {
		
		User user = (User) request.getSession().getAttribute("user");
		
//		String root = request.getSession().getServletContext().getRealPath("/");
		String root = "/tempfile/";;
		
		String fileName = file.getOriginalFilename();
		
		String filePath = root + "pdf" + File.separator + fileName; 
		
		File fileImport = new File(filePath);
		
		if (!fileImport.getParentFile().exists()) {
			fileImport.getParentFile().mkdirs();
		}
		
		try {
			file.transferTo(fileImport);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ResumeFile resumeFile = new ResumeFile();
		resumeFile.setCreateTime(new Date());
		resumeFile.setFileName(fileName);
		resumeFile.setFilePath(filePath);
		resumeFile.setResumeName(resumeName);
		resumeFile.setUserId(user.getId());
		resumeService.saveResumeFile(resumeFile);
		return filePath;
	}
	
	@RequestMapping("/pdf/del")
	@ResponseBody
	public String delPdfFile(@RequestParam("fileId")String fileId, HttpServletRequest request) {
		String root = "/tempfile/";
//		String root = request.getSession().getServletContext().getRealPath("/");
		
		ResumeFile resumeFile = resumeService.getResumeInfoByFileId(fileId);
		String fileName = resumeFile.getFileName();
		
		String filePath = root + "pdf" + File.separator + fileName; 
		
		new File(filePath).delete();
		
		int count = resumeService.delFile(fileId);
		
		return String.valueOf(count);
	}
}
